
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Bypass · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="distillery.html" />
    
    
    <link rel="prev" href="benchee.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="guardian.html">
            
                <a href="guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="poolboy.html">
            
                <a href="poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="benchee.html">
            
                <a href="benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="5.4" data-path="bypass.html">
            
                <a href="bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="distillery.html">
            
                <a href="distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="stream-data.html">
            
                <a href="stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Bypass</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x6D4B;&#x8BD5;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5F88;&#x591A;&#x60C5;&#x51B5;&#x4E0B;&#x6211;&#x4EEC;&#x8981;&#x5BF9;&#x5916;&#x90E8;&#x670D;&#x52A1;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#x3002;&#x6211;&#x4EEC;&#x751A;&#x81F3;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x6A21;&#x62DF;&#x4E0D;&#x540C;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x6BD4;&#x5982;&#x610F;&#x5916;&#x53D1;&#x751F;&#x7684;&#x670D;&#x52A1;&#x5668;&#x9519;&#x8BEF;&#x3002;&#x5728; Elixir &#x4E2D;&#x8981;&#x9AD8;&#x6548;&#x5730;&#x5B9E;&#x73B0;&#x4E0A;&#x9762;&#x7684;&#x529F;&#x80FD;&#x53EF;&#x5F97;&#x5411;&#x5916;&#x83B7;&#x53D6;&#x4E00;&#x4E9B;&#x5E2E;&#x52A9;&#x3002;  </p>
<p>&#x5728;&#x672C;&#x7AE0;&#x8BFE;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x63A2;&#x7D22; <a href="https://github.com/PSPDFKit-labs/bypass" target="_blank">bypass</a> &#x662F;&#x5982;&#x4F55;&#x5FEB;&#x901F;&#x65B9;&#x4FBF;&#x5730;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x5728;&#x6D4B;&#x8BD5;&#x4E2D;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x8BF7;&#x6C42;&#x3002;  </p>
<h2 id="bypass-&#x662F;&#x4EC0;&#x4E48;&#xFF1F;">Bypass &#x662F;&#x4EC0;&#x4E48;&#xFF1F;</h2>
<p><a href="https://github.com/PSPDFKit-labs/bypass" target="_blank">Bypass</a> &#x88AB;&#x4ECB;&#x7ECD;&#x4E3A; &#x201C;&#x53EF;&#x5FEB;&#x901F;&#x521B;&#x5EFA;&#x53D6;&#x4EE3;&#x5B9E;&#x9645; HTTP &#x670D;&#x52A1;&#x5668;&#x7684;&#x81EA;&#x5B9A;&#x4E49; plug&#xFF0C;&#x8FD4;&#x56DE;&#x9884;&#x5148;&#x5B9A;&#x4E49;&#x7ED3;&#x679C;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x5DE5;&#x5177;&#x201D;&#x3002;  </p>
<p>&#x8FD9;&#x662F;&#x4EC0;&#x4E48;&#x610F;&#x601D;&#xFF1F;Bypass &#x80CC;&#x540E;&#x5176;&#x5B9E;&#x662F;&#x4E00;&#x4E2A;&#x5047;&#x626E;&#x4E3A;&#x5916;&#x90E8;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x76D1;&#x542C;&#x8BF7;&#x6C42;&#x5E76;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x7684; OTP &#x5E94;&#x7528;&#x3002;&#x901A;&#x8FC7;&#x8FD4;&#x56DE;&#x9884;&#x5B9A;&#x4E49;&#x503C;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6D4B;&#x8BD5;&#x5404;&#x79CD;&#x53EF;&#x80FD;&#x6027;&#xFF0C;&#x5982;&#x6B63;&#x5E38;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x4EE5;&#x53CA;&#x610F;&#x5916;&#x7684;&#x670D;&#x52A1;&#x4E2D;&#x6B62;&#x548C;&#x9519;&#x8BEF;&#x3002;&#x8FD9;&#x90FD;&#x4E0D;&#x9700;&#x8981;&#x5411;&#x5916;&#x53D1;&#x51FA;&#x4EFB;&#x4F55;&#x8BF7;&#x6C42;&#x3002;  </p>
<h2 id="bypass-&#x7528;&#x6CD5;">Bypass &#x7528;&#x6CD5;</h2>
<p>&#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x5730;&#x5C55;&#x793A; Bypass &#x7684;&#x529F;&#x80FD;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x5DE5;&#x5177;&#x5E94;&#x7528;&#x3002;&#x5B83;&#x4F1A; ping &#x4E00;&#x4E9B;&#x57DF;&#x540D;&#x5E76;&#x786E;&#x4FDD;&#x5B83;&#x4EEC;&#x5728;&#x7EBF;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; supervisor &#x9879;&#x76EE;&#xFF0C;&#x548C;&#x4E00;&#x4E2A; GenServer&#xFF0C;&#x6309;&#x914D;&#x7F6E;&#x7684;&#x95F4;&#x9694;&#x68C0;&#x67E5;&#x8FD9;&#x4E9B;&#x57DF;&#x540D;&#x3002;&#x5728;&#x6D4B;&#x8BD5;&#x4E2D;&#x4F7F;&#x7528; Bypass &#x53EF;&#x4EE5;&#x786E;&#x4FDD;&#x5728;&#x4E0D;&#x540C;&#x60C5;&#x5F62;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x90FD;&#x80FD;&#x6B63;&#x5E38;&#x5DE5;&#x4F5C;&#x3002;  </p>
<p><em>&#x6CE8;&#x610F;</em>: &#x5982;&#x679C;&#x4F60;&#x5E0C;&#x671B;&#x83B7;&#x5F97;&#x6700;&#x7EC8;&#x7684;&#x5B8C;&#x6574;&#x4EE3;&#x7801;&#xFF0C;&#x53EF;&#x4EE5;&#x5230; Elixir School &#x7684; <a href="https://github.com/elixirschool/clinic" target="_blank">Clinic</a> &#x9879;&#x76EE;&#x67E5;&#x770B;&#x3002;  </p>
<p>&#x5047;&#x5B9A;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5BF9;&#x521B;&#x5EFA; Mix &#x9879;&#x76EE;&#xFF0C;&#x6DFB;&#x52A0;&#x4F9D;&#x8D56;&#x6BD4;&#x8F83;&#x719F;&#x6089;&#x4E86;&#xFF0C;&#x8FD9;&#x91CC;&#x5C31;&#x53EA;&#x5173;&#x6CE8;&#x5728;&#x8981;&#x6D4B;&#x8BD5;&#x7684;&#x4EE3;&#x7801;&#x4E0A;&#x3002;&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x7A0D;&#x5FAE;&#x505A;&#x4E9B;&#x56DE;&#x987E;&#xFF0C;&#x8BF7;&#x53C2;&#x8003;&#x6211;&#x4EEC; <a href="https://elixirschool.com/en/lessons/basics/mix" target="_blank">Mix</a> &#x8BFE;&#x7A0B;&#x7684; <a href="https://elixirschool.com/en/lessons/basics/mix/#new-projects" target="_blank">New Projects</a> &#x90E8;&#x5206;&#x3002;  </p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x5B83;&#x8D1F;&#x8D23;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x5230;&#x90A3;&#x4E9B;&#x57DF;&#x540D;&#x4E0A;&#x3002;&#x901A;&#x8FC7;&#x4F7F;&#x7528; <a href="https://github.com/edgurgel/httpoison" target="_blank">HTTPoison</a>&#xFF0C;&#x6211;&#x4EEC;&#x65B0;&#x5EFA;&#x7684;&#x51FD;&#x6570; <code>ping/1</code> &#x4F1A;&#x63A5;&#x6536;&#x4E00;&#x4E2A; URL &#x5E76;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#x3002;&#x5982;&#x679C;&#x8BF7;&#x6C42;&#x6210;&#x529F;&#xFF0C;&#x5E76;&#x63A5;&#x6536;&#x5230; HTTP 200 &#x72B6;&#x6001;&#x7801;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5C31;&#x8FD4;&#x56DE; <code>{:ok, body}</code>&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE; <code>{:error, reason}</code>&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Clinic</span></span>.HealthCheck <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">ping</span></span>(urls) <span class="hljs-keyword">when</span> is_list(urls), <span class="hljs-symbol">do:</span> Enum.map(urls, &amp;ping/<span class="hljs-number">1</span>)

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">ping</span></span>(url) <span class="hljs-keyword">do</span>
    url
    |&gt; HTTPoison.get()
    |&gt; response()
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">response</span></span>({<span class="hljs-symbol">:ok</span>, %{<span class="hljs-symbol">status_code:</span> <span class="hljs-number">200</span>, <span class="hljs-symbol">body:</span> body}}), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, body}
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">response</span></span>({<span class="hljs-symbol">:ok</span>, %{<span class="hljs-symbol">status_code:</span> status_code}}), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:error</span>, <span class="hljs-string">&quot;HTTP Status <span class="hljs-subst">#{status_code}</span>&quot;</span>}
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">response</span></span>({<span class="hljs-symbol">:error</span>, %{<span class="hljs-symbol">reason:</span> reason}}), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:error</span>, reason}
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x4F60;&#x53EF;&#x80FD;&#x53D1;&#x73B0;&#xFF0C;&#x6211;&#x5E76;<em>&#x6CA1;&#x6709;</em>&#x4F7F;&#x7528; GenServer&#x3002;&#x597D;&#x5904;&#x662F;&#xFF0C;&#x9694;&#x79BB;&#x5F00; GenServer &#x548C;&#x5B9E;&#x9645;&#x7684;&#x529F;&#x80FD;&#xFF08;&#x548C;&#x5173;&#x6CE8;&#x70B9;&#xFF09;&#xFF0C;&#x6211;&#x4EEC;&#x80FD;&#x5728;&#x4E0D;&#x5F15;&#x5165;&#x5E76;&#x884C;&#x5904;&#x7406;&#x7684;&#x590D;&#x6742;&#x5EA6;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6D4B;&#x8BD5;&#x6211;&#x4EEC;&#x7684;&#x4EE3;&#x7801;&#x3002;  </p>
<p>&#x5B9E;&#x9645;&#x4EE3;&#x7801;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x597D;&#xFF0C;&#x662F;&#x65F6;&#x5019;&#x5F00;&#x59CB;&#x6D4B;&#x8BD5;&#x4E86;&#x3002;&#x5728;&#x4F7F;&#x7528; Bypass &#x4EE5;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x8981;&#x786E;&#x4FDD;&#x5B83;&#x5728;&#x8FD0;&#x884C;&#x7684;&#x72B6;&#x6001;&#x3002;&#x5982;&#x4E0B;&#x4FEE;&#x6539; <code>test/test_helper.exs</code> &#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#xFF1A;  </p>
<pre><code class="lang-elixir">ExUnit.start()
Application.ensure_all_started(<span class="hljs-symbol">:bypass</span>)
</code></pre>
<p>&#x65E2;&#x7136; Bypass &#x5DF2;&#x7ECF;&#x80FD;&#x5728;&#x8DD1;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x7684;&#x65F6;&#x5019;&#x4FDD;&#x6301;&#x8FD0;&#x884C;&#xFF0C;&#x6211;&#x4EEC;&#x9A6C;&#x4E0A;&#x901A;&#x8FC7; <code>test/clinic/health_check_test.exs</code> &#x6765;&#x5B8C;&#x6210;&#x8BBE;&#x7F6E;&#x3002;&#x901A;&#x8FC7;&#x5728;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x7684; setup &#x56DE;&#x8C03;&#x4E2D;&#x4F7F;&#x7528; <code>Bypass.open/1</code> &#x6765;&#x6253;&#x5F00;&#x8FDE;&#x63A5;&#xFF0C;Bypass &#x5C31;&#x80FD;&#x591F;&#x63A5;&#x6536;&#x8BF7;&#x6C42;&#x4E86;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Clinic</span></span>.HealthCheckTests <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case

  setup <span class="hljs-keyword">do</span>
    bypass = Bypass.open()
    {<span class="hljs-symbol">:ok</span>, <span class="hljs-symbol">bypass:</span> bypass}
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x6682;&#x4E14;&#x4F7F;&#x7528; Bypass &#x7684;&#x9ED8;&#x8BA4;&#x7AEF;&#x53E3;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x6709;&#x9700;&#x8981;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B; <code>:port</code> &#x9009;&#x9879;&#x7ED9; <code>Bypass.open/1</code> &#x51FD;&#x6570;&#xFF0C;&#x4F8B;&#x5982; <code>Bypass.open(port: 1337)</code>&#xFF08;&#x4E0B;&#x4E00;&#x7AE0;&#x8282;&#x6211;&#x4EEC;&#x5C31;&#x4F1A;&#x8FD9;&#x4E48;&#x505A;&#xFF09;&#x3002;&#x73B0;&#x5728; Bypass &#x5DF2;&#x7ECF;&#x53EF;&#x4EE5;&#x5DE5;&#x4F5C;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x53D1;&#x8D77;&#x4E00;&#x4E2A;&#x6210;&#x529F;&#x7684;&#x8BF7;&#x6C42;&#x770B;&#x770B;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Clinic</span></span>.HealthCheckTests <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case

  <span class="hljs-keyword">alias</span> Clinic.HealthCheck

  setup <span class="hljs-keyword">do</span>
    bypass = Bypass.open()
    {<span class="hljs-symbol">:ok</span>, <span class="hljs-symbol">bypass:</span> bypass}
  <span class="hljs-keyword">end</span>

  test <span class="hljs-string">&quot;request with HTTP 200 response&quot;</span>, %{<span class="hljs-symbol">bypass:</span> bypass} <span class="hljs-keyword">do</span>
    Bypass.expect(bypass, <span class="hljs-keyword">fn</span> conn -&gt;
      Plug.Conn.resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;pong&quot;</span>)
    <span class="hljs-keyword">end</span>)

    assert {<span class="hljs-symbol">:ok</span>, <span class="hljs-string">&quot;pong&quot;</span>} = HealthCheck.ping(<span class="hljs-string">&quot;http://localhost:<span class="hljs-subst">#{bypass.port}</span>&quot;</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x7684;&#x6D4B;&#x8BD5;&#x5E94;&#x8BE5;&#x5DF2;&#x7ECF;&#x8DB3;&#x591F;&#x7B80;&#x5355;&#xFF0C;&#x8FD0;&#x884C;&#x540E;&#x5E94;&#x8BE5;&#x80FD;&#x770B;&#x5230;&#x901A;&#x8FC7;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x8FD8;&#x662F;&#x6DF1;&#x5165;&#x4E86;&#x89E3;&#x6BCF;&#x4E00;&#x90E8;&#x5206;&#x5230;&#x5E95;&#x505A;&#x4E86;&#x4EC0;&#x4E48;&#x3002;&#x7B2C;&#x4E00;&#x90E8;&#x5206;&#x662F; <code>Bypass.expect/2</code> &#x51FD;&#x6570;&#xFF1A;  </p>
<pre><code class="lang-elixir">Bypass.expect(bypass, <span class="hljs-keyword">fn</span> conn -&gt;
  Plug.Conn.resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;pong&quot;</span>)
<span class="hljs-keyword">end</span>)
</code></pre>
<p><code>Bypass.expect/2</code> &#x63A5;&#x6536;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;Bypass &#x8FDE;&#x63A5;&#x548C;&#x5355;&#x53C2;&#x6570;&#x7684;&#x51FD;&#x6570;&#x3002;&#x8FD9;&#x4E2A;&#x5355;&#x53C2;&#x6570;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x8FDE;&#x63A5;&#x5E76;&#x8FD4;&#x56DE;&#x3002;&#x5B83;&#x540C;&#x65F6;&#x4E5F;&#x53EF;&#x4EE5;&#x68C0;&#x67E5;&#x8BF7;&#x6C42;&#x662F;&#x5426;&#x7B26;&#x5408;&#x6211;&#x4EEC;&#x7684;&#x671F;&#x671B;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x4FEE;&#x6539;&#x6D4B;&#x8BD5;&#x7684; url&#xFF0C;&#x4F7F;&#x4E4B;&#x5305;&#x542B; <code>/ping</code>&#xFF0C;&#x5E76;&#x4E14;&#x9A8C;&#x8BC1;&#x8BF7;&#x6C42;&#x7684;&#x8DEF;&#x5F84;&#x548C; HTTP method&#xFF1A;  </p>
<pre><code class="lang-elixir">test <span class="hljs-string">&quot;request with HTTP 200 response&quot;</span>, %{<span class="hljs-symbol">bypass:</span> bypass} <span class="hljs-keyword">do</span>
  Bypass.expect(bypass, <span class="hljs-keyword">fn</span> conn -&gt;
    assert <span class="hljs-string">&quot;GET&quot;</span> == conn.method
    assert <span class="hljs-string">&quot;/ping&quot;</span> == conn.request_path
    Plug.Conn.resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;pong&quot;</span>)
  <span class="hljs-keyword">end</span>)

  assert {<span class="hljs-symbol">:ok</span>, <span class="hljs-string">&quot;pong&quot;</span>} = HealthCheck.ping(<span class="hljs-string">&quot;http://localhost:<span class="hljs-subst">#{bypass.port}</span>/ping&quot;</span>)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6D4B;&#x8BD5;&#x7684;&#x6700;&#x540E;&#x90E8;&#x5206;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E86; <code>HealthCheck.ping/1</code> &#x5E76;&#x65AD;&#x8A00;&#x8FD4;&#x56DE;&#x548C;&#x671F;&#x671B;&#x503C;&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x662F;&#x4E3A;&#x4EC0;&#x4E48;&#x4F1A;&#x5F15;&#x7528; <code>bypass.port</code> &#x5462;&#xFF1F;Bypass &#x5B9E;&#x9645;&#x4E0A;&#x76D1;&#x542C;&#x4E86;&#x672C;&#x5730;&#x7684;&#x4E00;&#x4E2A;&#x7AEF;&#x53E3;&#xFF0C;&#x5E76;&#x62E6;&#x622A;&#x4E86;&#x6240;&#x6709;&#x7684;&#x8BF7;&#x6C42;&#x3002;&#x4F7F;&#x7528; <code>bypass.port</code> &#x5C31;&#x662F;&#x83B7;&#x53D6;&#x9ED8;&#x8BA4;&#x7684;&#x7AEF;&#x53E3;&#x503C;&#xFF0C;&#x56E0;&#x4E3A;&#x4F7F;&#x7528; <code>Bypass.open/1</code> &#x7684;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x5E76;&#x6CA1;&#x6709;&#x6307;&#x660E;&#x7AEF;&#x53E3;&#x3002;  </p>
<p>&#x4E0B;&#x4E00;&#x6B65;&#x5C31;&#x6DFB;&#x52A0;&#x9519;&#x8BEF;&#x573A;&#x666F;&#x7684;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x50CF;&#x524D;&#x9762;&#x7684;&#x6D4B;&#x8BD5;&#x90A3;&#x6837;&#xFF0C;&#x505A;&#x51FA;&#x7EC6;&#x5FAE;&#x7684;&#x6539;&#x53D8;&#x5C31;&#x884C;&#x4E86;&#xFF1A;&#x8FD4;&#x56DE; 500 &#x4F5C;&#x4E3A;&#x72B6;&#x6001;&#x7801;&#xFF0C;&#x5E76;&#x4E14;&#x65AD;&#x8A00; <code>{:error, reason}</code> &#x5143;&#x7EC4;&#x5C31;&#x662F;&#x8FD4;&#x56DE;&#x503C;&#xFF1A;  </p>
<pre><code class="lang-elixir">test <span class="hljs-string">&quot;request with HTTP 500 response&quot;</span>, %{<span class="hljs-symbol">bypass:</span> bypass} <span class="hljs-keyword">do</span>
  Bypass.expect(bypass, <span class="hljs-keyword">fn</span> conn -&gt;
    Plug.Conn.resp(conn, <span class="hljs-number">500</span>, <span class="hljs-string">&quot;Server Error&quot;</span>)
  <span class="hljs-keyword">end</span>)

  assert {<span class="hljs-symbol">:error</span>, <span class="hljs-string">&quot;HTTP Status 500&quot;</span>} = HealthCheck.ping(<span class="hljs-string">&quot;http://localhost:<span class="hljs-subst">#{bypass.port}</span>&quot;</span>)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x6CA1;&#x4EC0;&#x4E48;&#x7279;&#x522B;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x7EE7;&#x7EED;&#x4E0B;&#x4E00;&#x4E2A;&#x610F;&#x5916;&#x670D;&#x52A1;&#x4E2D;&#x65AD;&#x7684;&#x573A;&#x666F;&#x5427;&#x3002;&#x8FD9;&#x4E9B;&#x60C5;&#x51B5;&#x7684;&#x8BF7;&#x6C42;&#x662F;&#x6211;&#x4EEC;&#x6700;&#x62C5;&#x5FC3;&#x7684;&#x5730;&#x65B9;&#x3002;&#x6211;&#x4EEC;&#x4E0D;&#x4F1A;&#x4F7F;&#x7528; <code>Bypass.expect/2</code> &#x6765;&#x6A21;&#x62DF;&#x5B9E;&#x73B0;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x800C;&#x662F;&#x4F9D;&#x9760; <code>Bypass.down/1</code> &#x6765;&#x4E2D;&#x65AD;&#x8FDE;&#x63A5;&#xFF1A;  </p>
<pre><code class="lang-elixir">test <span class="hljs-string">&quot;request with unexpected outage&quot;</span>, %{<span class="hljs-symbol">bypass:</span> bypass} <span class="hljs-keyword">do</span>
  Bypass.down(bypass)

  assert {<span class="hljs-symbol">:error</span>, <span class="hljs-symbol">:econnrefused</span>} = HealthCheck.ping(<span class="hljs-string">&quot;http://localhost:<span class="hljs-subst">#{bypass.port}</span>&quot;</span>)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5982;&#x679C;&#x8FD0;&#x884C;&#x6211;&#x4EEC;&#x7684;&#x65B0;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x53D1;&#x73B0;&#x6240;&#x6709;&#x7684;&#x90FD;&#x901A;&#x8FC7;&#x4E86;&#xFF01;&#x65E2;&#x7136; <code>HealthCheck</code> &#x6A21;&#x5757;&#x5DF2;&#x7ECF;&#x901A;&#x8FC7;&#x4E86;&#x6D4B;&#x8BD5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7EE7;&#x7EED;&#x6D4B;&#x8BD5;&#x57FA;&#x4E8E; GenServer &#x7684;&#x5B9A;&#x65F6;&#x4EFB;&#x52A1;&#x4E86;&#x3002;  </p>
<h2 id="&#x591A;&#x4E2A;&#x5916;&#x90E8;&#x57DF;&#x540D;">&#x591A;&#x4E2A;&#x5916;&#x90E8;&#x57DF;&#x540D;</h2>
<p>&#x5BF9;&#x4E8E;&#x6211;&#x4EEC;&#x9879;&#x76EE;&#x6765;&#x8BF4;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x4FDD;&#x6301;&#x5B9A;&#x65F6;&#x4EFB;&#x52A1;&#x7684;&#x6838;&#x5FC3;&#xFF0C;&#x4F9D;&#x8D56; <code>Process.send_after/3</code> &#x6765;&#x89E6;&#x53D1;&#x4E0D;&#x65AD;&#x91CD;&#x590D;&#x7684;&#x68C0;&#x67E5;&#x3002;&#x8981;&#x4E86;&#x89E3;&#x66F4;&#x591A;&#x5173;&#x4E8E; <code>Process</code> &#x6A21;&#x5757;&#xFF0C;&#x8BF7;&#x53C2;&#x8003;<a href="https://hexdocs.pm/elixir/Process.html" target="_blank">&#x6587;&#x6863;</a>&#x3002;&#x6211;&#x4EEC;&#x7684;&#x5B9A;&#x65F6;&#x4EFB;&#x52A1;&#x9700;&#x8981;&#x4E09;&#x4E2A;&#x914D;&#x7F6E;&#x53C2;&#x6570;&#xFF1A;&#x7F51;&#x7AD9;&#x5217;&#x8868;&#xFF0C;&#x5B9A;&#x65F6;&#x68C0;&#x67E5;&#x7684;&#x95F4;&#x9694;&#x65F6;&#x95F4;&#xFF0C;&#x548C;&#x5B9E;&#x73B0;&#x4E86; <code>ping/1</code> &#x51FD;&#x6570;&#x7684;&#x6A21;&#x5757;&#x3002;&#x901A;&#x8FC7;&#x4F20;&#x5165;&#x5B9E;&#x73B0;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x6211;&#x4EEC;&#x628A;&#x529F;&#x80FD;&#x548C; GenServer &#x89E3;&#x8026;&#x5F00;&#xFF0C;&#x4F7F;&#x6211;&#x4EEC;&#x66F4;&#x597D;&#x5730;&#x72EC;&#x7ACB;&#x6D4B;&#x8BD5;&#x5B83;&#x4EEC;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(opts) <span class="hljs-keyword">do</span>
  sites = Keyword.fetch!(opts, <span class="hljs-symbol">:sites</span>)
  interval = Keyword.fetch!(opts, <span class="hljs-symbol">:interval</span>)
  health_check = Keyword.get(opts, <span class="hljs-symbol">:health_check</span>, HealthCheck)

  Process.send_after(<span class="hljs-keyword">self</span>(), <span class="hljs-symbol">:check</span>, interval)

  {<span class="hljs-symbol">:ok</span>, {health_check, sites}}
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5B9A;&#x4E49; <code>handle_info/2</code> &#x51FD;&#x6570;&#x63A5;&#x6536; <code>send_after/2</code> &#x53D1;&#x9001;&#x7684; <code>:check</code> &#x6D88;&#x606F;&#x3002;&#x7B80;&#x5355;&#x671F;&#x95F4;&#xFF0C;&#x6211;&#x4EEC;&#x628A;&#x7F51;&#x7AD9;&#x5217;&#x8868;&#x4F20;&#x7ED9; <code>HealthCheck.ping/1</code>&#xFF0C;&#x5E76;&#x4E14;&#x628A;&#x7ED3;&#x679C;&#x901A;&#x8FC7; <code>Logger.info</code> &#x8BB0;&#x5F55;&#x8D77;&#x6765;&#xFF0C;&#x6216;&#x8005;&#x7528; <code>Logger.error</code> &#x8BB0;&#x5F55;&#x9519;&#x8BEF;&#x3002;&#x8FDF;&#x4E9B;&#x6211;&#x4EEC;&#x4F1A;&#x518D;&#x6539;&#x8FDB;&#x6211;&#x4EEC;&#x4EE3;&#x7801;&#x91CC;&#x7684;&#x65E5;&#x5FD7;&#x8BB0;&#x5F55;&#x529F;&#x80FD;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">handle_info</span></span>(<span class="hljs-symbol">:check</span>, {health_check, sites}) <span class="hljs-keyword">do</span>
  sites
  |&gt; health_check.ping()
  |&gt; Enum.each(&amp;report/<span class="hljs-number">1</span>)

  {<span class="hljs-symbol">:noreply</span>, {health_check, sites}}
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">report</span></span>({<span class="hljs-symbol">:ok</span>, body}), <span class="hljs-symbol">do:</span> Logger.info(body)
<span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">report</span></span>({<span class="hljs-symbol">:error</span>, reason}) <span class="hljs-keyword">do</span>
  reason
  |&gt; to_string()
  |&gt; Logger.error()
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5982;&#x524D;&#x6240;&#x8FF0;&#xFF0C;&#x6211;&#x4EEC;&#x628A;&#x7F51;&#x7AD9;&#x5217;&#x8868;&#x4F20;&#x7ED9; <code>HealthCheck.ping/1</code>&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7; <code>Enum.each/2</code> &#x904D;&#x5386;&#x5B83;&#x4EEC;&#x5E76;&#x5E94;&#x7528; <code>report/1</code> &#x51FD;&#x6570;&#x5230;&#x6BCF;&#x4E2A;&#x7F51;&#x7AD9;&#x4E0A;&#x3002;&#x6709;&#x4E86;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;&#xFF0C;&#x5B9A;&#x65F6;&#x4EFB;&#x52A1;&#x5C31;&#x5B8C;&#x6210;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x5C31;&#x53EF;&#x4EE5;&#x5173;&#x6CE8;&#x5728;&#x6D4B;&#x8BD5;&#x4E0A;&#x9762;&#x4E86;&#x3002;  </p>
<p>&#x6211;&#x4EEC;&#x4E0D;&#x4F1A;&#x5728;&#x5B9A;&#x65F6;&#x4EFB;&#x52A1;&#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x4E0A;&#x9762;&#x82B1;&#x592A;&#x591A;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x56E0;&#x4E3A;&#x90A3;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x5230; Bypass&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x6700;&#x7EC8;&#x7684;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x662F;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Clinic</span></span>.SchedulerTest <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case

  import ExUnit.CaptureLog

  <span class="hljs-keyword">alias</span> Clinic.Scheduler

  <span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">TestCheck</span></span> <span class="hljs-keyword">do</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">ping</span></span>(_sites), <span class="hljs-symbol">do:</span> [{<span class="hljs-symbol">:ok</span>, <span class="hljs-string">&quot;pong&quot;</span>}, {<span class="hljs-symbol">:error</span>, <span class="hljs-string">&quot;HTTP Status 404&quot;</span>}]
  <span class="hljs-keyword">end</span>

  test <span class="hljs-string">&quot;health checks are run and results logged&quot;</span> <span class="hljs-keyword">do</span>
    opts = [<span class="hljs-symbol">health_checks:</span> TestCheck, <span class="hljs-symbol">interval:</span> <span class="hljs-number">1</span>, <span class="hljs-symbol">sites:</span> [<span class="hljs-string">&quot;http://example.com&quot;</span>, <span class="hljs-string">&quot;http://example.org&quot;</span>]]

    output =
      capture_log(<span class="hljs-keyword">fn</span> -&gt;
        {<span class="hljs-symbol">:ok</span>, _pid} = GenServer.start_link(Scheduler, opts)
        <span class="hljs-symbol">:timer</span>.sleep(<span class="hljs-number">10</span>)
      <span class="hljs-keyword">end</span>)

    assert output =~ <span class="hljs-string">&quot;pong&quot;</span>
    assert output =~ <span class="hljs-string">&quot;HTTP Status 404&quot;</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x4F9D;&#x8D56; <code>TestCheck</code> &#x5B9E;&#x73B0;&#x7F51;&#x7AD9;&#x7684;&#x5065;&#x5EB7;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#xFF0C;&#x548C;&#x4F7F;&#x7528; <code>CaptureLog.capture_log/1</code> &#x6765;&#x65AD;&#x8A00;&#x76F8;&#x5E94;&#x7684;&#x65E5;&#x5FD7;&#x4FE1;&#x606F;&#x6709;&#x88AB;&#x8BB0;&#x5F55;&#x4E0B;&#x6765;&#x3002;  </p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x628A; <code>Scheduler</code> &#x548C; <code>HealthCheck</code> &#x51C6;&#x5907;&#x597D;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5F00;&#x59CB;&#x5199;&#x96C6;&#x6210;&#x6D4B;&#x8BD5;&#x6765;&#x786E;&#x4FDD;&#x6240;&#x6709;&#x7684;&#x90E8;&#x4EF6;&#x90FD;&#x80FD;&#x6B63;&#x786E;&#x8FD0;&#x884C;&#x3002;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x91CC;&#x9762;&#x4F7F;&#x7528; Bypass &#x6765;&#x6D4B;&#x8BD5;&#x591A;&#x4E2A;&#x8BF7;&#x6C42;&#x3002;  </p>
<p>&#x8FD8;&#x8BB0;&#x5F97;&#x4E4B;&#x524D;&#x7684; <code>bypass.port</code> &#x5417;&#xFF1F;&#x90FD;&#x80FD;&#x591F;&#x6211;&#x4EEC;&#x6A21;&#x62DF;&#x591A;&#x4E2A;&#x7F51;&#x7AD9;&#x7684;&#x65F6;&#x5019;&#xFF0C;<code>:port</code> &#x9009;&#x9879;&#x662F;&#x5F88;&#x597D;&#x7528;&#x7684;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x5DF2;&#x7ECF;&#x731C;&#x5230;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x591A;&#x4E2A; Bypass &#x8FDE;&#x63A5;&#xFF0C;&#x6BCF;&#x4E2A;&#x4F7F;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x7AEF;&#x53E3;&#xFF0C;&#x76F8;&#x5E94;&#x5730;&#x6A21;&#x62DF;&#x5404;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;&#x7F51;&#x7AD9;&#x3002;&#x6700;&#x65B0;&#x7684;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6; <code>test/clinic_test.exs</code> &#x5E94;&#x8BE5;&#x5982;&#x4E0B;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">ClinicTest</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case

  import ExUnit.CaptureLog

  <span class="hljs-keyword">alias</span> Clinic.Scheduler

  test <span class="hljs-string">&quot;sites are checked and results logged&quot;</span> <span class="hljs-keyword">do</span>
    bypass_one = Bypass.open(<span class="hljs-symbol">port:</span> <span class="hljs-number">1234</span>)
    bypass_two = Bypass.open(<span class="hljs-symbol">port:</span> <span class="hljs-number">1337</span>)

    Bypass.expect(bypass_one, <span class="hljs-keyword">fn</span> conn -&gt;
      Plug.Conn.resp(conn, <span class="hljs-number">500</span>, <span class="hljs-string">&quot;Server Error&quot;</span>)
    <span class="hljs-keyword">end</span>)

    Bypass.expect(bypass_two, <span class="hljs-keyword">fn</span> conn -&gt;
      Plug.Conn.resp(conn, <span class="hljs-number">200</span>, <span class="hljs-string">&quot;pong&quot;</span>)
    <span class="hljs-keyword">end</span>)

    opts = [<span class="hljs-symbol">interval:</span> <span class="hljs-number">1</span>, <span class="hljs-symbol">sites:</span> [<span class="hljs-string">&quot;http://localhost:1234&quot;</span>, <span class="hljs-string">&quot;http://localhost:1337&quot;</span>]]

    output =
      capture_log(<span class="hljs-keyword">fn</span> -&gt;
        {<span class="hljs-symbol">:ok</span>, _pid} = GenServer.start_link(Scheduler, opts)
        <span class="hljs-symbol">:timer</span>.sleep(<span class="hljs-number">10</span>)
      <span class="hljs-keyword">end</span>)

    assert output =~ <span class="hljs-string">&quot;[info]  pong&quot;</span>
    assert output =~ <span class="hljs-string">&quot;[error] HTTP Status 500&quot;</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x7684;&#x6D4B;&#x8BD5;&#x5E94;&#x8BE5;&#x6CA1;&#x6709;&#x592A;&#x591A;&#x503C;&#x5F97;&#x60CA;&#x5947;&#x7684;&#x5730;&#x65B9;&#x3002;&#x4E0E;&#x5176;&#x5728; <code>setup</code> &#x521B;&#x5EFA;&#x4E00;&#x4E2A; Bypass &#x8FDE;&#x63A5;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x4E2D;&#x521B;&#x5EFA;&#x4E86;&#x4E24;&#x4E2A;&#x7AEF;&#x53E3;&#x5206;&#x522B;&#x4E3A; 1234 &#x548C; 1337 &#x7684;&#x8FDE;&#x63A5;&#x3002;&#x7136;&#x540E;&#xFF0C;&#x8C03;&#x7528; <code>Bypass.expect/2</code> &#x6A21;&#x62DF;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x8FD4;&#x56DE;&#x3002;&#x6700;&#x540E;&#xFF0C;&#x518D;&#x52A0;&#x4E0A; <code>SchedulerTest</code> &#x4E2D;&#x542F;&#x52A8;&#x8BA1;&#x5212;&#x4EFB;&#x52A1;&#x548C;&#x65AD;&#x8A00;&#x4FE1;&#x606F;&#x7684;&#x76F8;&#x540C;&#x7684;&#x4EE3;&#x7801;&#x3002;  </p>
<p>&#x5927;&#x529F;&#x544A;&#x6210;&#xFF01;&#x4E00;&#x4E2A;&#x6301;&#x7EED;&#x76D1;&#x63A7;&#x4E00;&#x4E9B;&#x57DF;&#x540D;&#xFF0C;&#x5E76;&#x6C47;&#x62A5;&#x4EFB;&#x4F55;&#x95EE;&#x9898;&#x7684;&#x5DE5;&#x5177;&#x5C31;&#x6253;&#x9020;&#x5B8C;&#x6210;&#x4E86;&#x3002;&#x6211;&#x4EEC;&#x8FD8;&#x5B66;&#x4F1A;&#x5982;&#x4F55;&#x4F7F;&#x7528; Bypass &#x6765;&#x66F4;&#x597D;&#x5730;&#x7F16;&#x5199;&#x6D4B;&#x8BD5;&#x5916;&#x90E8;&#x670D;&#x52A1;&#x7684;&#x4EE3;&#x7801;&#x3002;  </p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="benchee.html" class="navigation navigation-prev " aria-label="Previous page: Benchee">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="distillery.html" class="navigation navigation-next " aria-label="Next page: Distillery(基础)">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.0","title":"Bypass","level":"5.4","depth":1,"next":{"title":"Distillery(基础)","level":"5.5","depth":1,"path":"lessons/libraries/distillery.md","ref":"lessons/libraries/distillery.md","articles":[]},"previous":{"title":"Benchee","level":"5.3","depth":1,"path":"lessons/libraries/benchee.md","ref":"lessons/libraries/benchee.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/libraries/bypass.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

